const i18n = new VueI18n({
    locale: 'zh', // 设置默认
    messages
});

var em = new Vue({
    el: main,
    i18n,
    data: {
        leftNum: 0, //用来判断左侧菜单的样式，选取0位第一个
        menuLefts: [{ //构建的左侧菜单选项
            title: "homeSystem.User_Management",
            url: "../img/home/IconUserS.png"
        },/* {
            title: "homeSystem.Crawler_management",
            url: "../img/home/crawler.png"
        }, {
            title: "homeSystem.Image_management",
            url: "../img/home/crawlerImage.png"
        }, {
            title: "homeSystem.Hardware_access",
            url: "../img/home/iconHardware.png"
        }, {
            title: "homeSystem.Threshold",
            url: "../img/home/Threshold.png"
        }, {
            title: "homeSystem.readSet",
            url: "../img/home/obseData.png"
        }*/],

        //分页
        totalCont: 0,
        pageNo: 1,
        //用户
        dialogFormVisible: false,//修改弹窗
        queryText: "",//查询
        userTableShow: true,
        usertableData: null, //模拟表格数据
        userQueryShow: true,//搜索栏 的显示和隐藏
        UserFormUpdata: {  //修改里面的
            id: "",
            userName: "",
            name: '',
            email: '',
            passWord: "",
            nickName: "",
            sex: "0",
            phone: "",
            usertype:"1"
        },
        formLabelWidth: '50px',
        //爬虫
        crawlerTableShow: false,
        crawlerTableData: null,
        activeCrlawerFlose: false,
        crawlerButText: "开启爬取网站数据",
        crawlerDialogFormVisible: false,
        formCrawlerUpdate: {  //修改里面的
            id: "",
            carwlerType: "",
            crawlerTime: null,
            crawlerTitle: "",
        },
        crawlerOptions: [{
            value: '一小时一次',
        }, {
            value: '一天一次',
        }, {
            value: '一年一次',
        }],
        //影像数据下载
        imageTableShow: false,
        imageTableData: null,
        imageQueryShow: false,
        imageButText: "开始卫星影像下载",
        activeImageFlose: false,
        imageDialogFormVisible: false,
        formImageUpdate: {
            id: "",
            crawlerRemarks: "",
            ftpRemark: "",
            lonUp: "",
            lonDown: "",
            latLeft: "",
            latRight: "",
        },
        /////硬件
        hardwareTableData: null,
        hardwareTableShow: false,
        hardwareButText: "开启一次爬取硬件接入",
        activeHardwareFlose: false,
        circleUrl: "../img/home/offline_line.png",
        locals18n: "",
        CrawlerFlose: 0,
        HardwareFlose: 0,

        //属性表
        proTableShow: false,
        proLayer: false,
        proFromUpdata: {
            proNamech: "",
            proRed: "",
            proYellow: "",
            proGreen: "",
            proUnit: "",
            proFiletype: "",
        },
        footShow: true,

        //红色范围
        redRange: [{}],
        yellowRange: [{}],
        greenRange: [{}],
        
      //读取设置
        proSetTableShow: false,
        proLayer: false,
        readSetData: null,
        footShow: true,

    },
    created: function () {
        //初始化
        this.ajaxData(1);
        try {
            window.parent.toggleLoading();
        } catch (e) {
        }
        this.locals18n = this.common.Initial();
        this.$i18n.locale = this.locals18n;

        //“开启卫星下载”的翻译
        if (this.locals18n == "en") {
            this.crawlerButText = messages.en.homeSystem.Start_crawling_website_data;
            this.imageButText = messages.en.homeSystem.Start_satellite_image_download;
            this.hardwareButText = messages.en.homeSystem.Enable_crawling_hardware_access_data;
        }
        console.log(this.hardwareButText);
    }, watch: {
        'CrawlerFlose': { //监控文件选择
            handler(newValue, oldValue) {
                if (this.locals18n == "en") {
                    if (newValue == 1) {
                        this.crawlerButText = messages.en.homeSystem.Turn_off_crawling_website_data;
                    } else {
                        this.crawlerButText = messages.en.homeSystem.Start_crawling_website_data;
                    }
                } else {
                    if (newValue == 1) {
                        this.crawlerButText = messages.zh.homeSystem.Turn_off_crawling_website_data;
                    } else {
                        this.crawlerButText = messages.zh.homeSystem.Start_crawling_website_data;
                    }
                }
            }
        }, 'HardwareFlose': { //监控文件选择
            handler(newValue, oldValue) {
                if (this.locals18n == "en") {
                    if (newValue == 1) {
                        this.hardwareButText = messages.en.homeSystem.Turn_off_crawling_hardware_access_data;
                    } else {
                        this.hardwareButText = messages.en.homeSystem.Enable_crawling_hardware_access_data;
                    }
                } else {
                    if (newValue == 1) {
                        this.hardwareButText = messages.zh.homeSystem.Turn_off_crawling_hardware_access_data;
                    } else {
                        this.hardwareButText = messages.zh.homeSystem.Enable_crawling_hardware_access_data;
                    }
                }
            }
        }
    },
    methods: {
        offOnline() {
            //离线下载
            // window.location.href = '../offLine/downZip.do';
            window.open("../offLine/downZip.do");
        },
        leftSwitch(index) { //上方菜单栏切换事件
            this.leftNum = index;
            for (i = 0; i < this.menuLefts.length; i++) {
                this.menuLefts[i].url = this.menuLefts[i].url.replace("S.png", ".png");
            }
            this.menuLefts[index].url = this.menuLefts[index].url.replace(".png", "S.png");
            this.crawlerFloseNumber();//判断是否可以点击爬虫
            //首先全部false
            this.userTableShow = false;
            this.crawlerTableShow = false;
            this.imageTableShow = false;
            this.hardwareTableShow = false;
            this.proTableShow = false;
            this.proSetTableShow = false;
            //然后在逐一打开
            switch (index) {
                case 0:
                    //用户
                    this.footShow = true;
                    this.userTableShow = true;
                    this.ajaxData(1);
                    break;
                case 1:
                    this.footShow = false;
                    //爬虫
                    this.crawlerTableShow = true;
                    this.crawlerTableajax();
                    break;
                case 2:
                    //影像下载管理
                    this.footShow = false;
                    this.imageTableShow = true;
                    this.imageTableajax();
                    break;
                case 3:
                    //硬件接入
                    this.footShow = false;
                    this.hardwardTableajax();
                    this.hardwareTableShow = true;
                    break;
                case 4:
                    //属性修改
                    this.footShow = true;
                    this.proTableajax();
                    this.proTableShow = true;
                    break;
                case 5:
                    //属性修改
                    this.footShow = false;
                    this.readSet();
                    this.proSetTableShow = true;
                    break;
            }
        },
        crawlerTableajax() {
            //爬虫表格
            // this.pageNo = 1;
            this.$http.get('../crawler/getPageCrawler.do?pageNo=' + this.pageNo, {emulateJSON: true}).then(function (res) {
                this.crawlerTableData = res.data.list;
                this.totalCont = res.data.count;
            });
        },
        imageSweet(stats, row) {
            //影像修改
            var statsText = "";
            if ("en" == this.locals18n && stats == 0) {
                statsText = messages.en.homeSystem.Startup_prompt;
            } else if ("en" == this.locals18n && stats == 1) {
                statsText = messages.en.homeSystem.stop_prompt;
            } else if ("zh" == this.locals18n && stats == 0) {
                statsText = messages.zh.homeSystem.Startup_prompt;
            } else if ("zh" == this.locals18n && stats == 1) {
                statsText = messages.zh.homeSystem.stop_prompt;
            }
            this.$confirm(statsText, '', {
                confirmButtonText: 'ok',
                cancelButtonText: 'cancel',
                type: 'warning'
            }).then(() => {
                // row.downloadState = stats;
                // delete row.beginTime;
                // console.log("？？？？？");
                // console.log(row);
                // this.$http.post('../crawler/updateImage.do', row, {emulateJSON: true}).then(function (res) {
                //     this.$message({
                //         message: '成功！',
                //         type: 'success'
                //     });
                // });
            }).catch();
        },
       
        updateState(stats, id) {
        		
        	
        	var statsText = "";
            if ("en" == this.locals18n && stats == 0) {
                statsText = messages.en.homeSystem.set_promptB;
            } else if ("en" == this.locals18n && stats == 1) {
                statsText = messages.en.homeSystem.set_promptA;
            } else if ("zh" == this.locals18n && stats == 0) {
                statsText = messages.zh.homeSystem.set_promptB;
            } else if ("zh" == this.locals18n && stats == 1) {
                statsText = messages.zh.homeSystem.set_promptA;
            }
            this.$confirm(statsText, '', {
                confirmButtonText: 'ok',
                cancelButtonText: 'cancel',
                type: 'warning'
            }).then(() => {
                this.$http.get('../setRead/update.do?id='+id+'&state='+stats, {emulateJSON: true}).then(function (res) {
                    this.readSet();
                    this.$message({
                        type: 'success',
                        message: '成功!'
                    });
                });
            }).catch();
            
        },
        imageTableajax() {
            //影像表格
            // this.pageNo = 1;
            this.$http.get('../crawler/getPageImageCrawler.do?pageNo=' + this.pageNo, {emulateJSON: true}).then(function (res) {
                console.log(res);
                this.imageTableData = res.data.list;
                this.totalCont = res.data.count;
            });
        }, readSet() {
            //读取设置
            // this.pageNo = 1;
            this.$http.get('../setRead/query.do?pageNo=' + this.pageNo, {emulateJSON: true}).then(function (res) {
                console.log(res);
                this.readSetData = res.data.list;
                this.totalCont = res.data.count;
            });
        },hardwardTableajax() {
            //硬件表格
            // this.pageNo = 1;
            this.$http.get('../hardware/getHarewaredAll.do', {emulateJSON: true}).then(function (res) {
                this.hardwareTableData = res.data;
            });
        }, proTableajax() {
            //属性表格
            // this.pageNo = 1;
            console.log(this.pageNo);
            this.$http.get('../readMap/findPagePro.do?languageType=' + this.locals18n + '&pageNo=' + this.pageNo, {emulateJSON: true}).then(function (res) {
                console.log(res);
                this.proLayer = false;
                this.usertableData = res.data.list;
                this.totalCont = res.data.count;
            });
        }, dateFormatData(row, column) {
            //表格上面格式化时间
            var date = row[column.property];
            if (date == null) {
                return "";
            }
            return new Date(parseInt(date)).toLocaleString().replace(/:\d{1,2}$/, ' ');
        }, LongLat(row, column) {
            //表格上面格式化时间
            if (row.latLeft == 0) {
                return " ";
            }
            // return  row.lonUp+","+row.lonDown+";"+row.latLeft+","+row.latRight;
            return row.latLeft + "°E," + row.lonDown + "°N--" + row.latRight + "°E," + row.lonUp + "°N ";
        },
        crawlerState(row, column) {
            //表格上面格式化状态
            var state = row[column.property];
            // console.log(state);
            return this.reallyTranslations(state);
        },
        handleCurrentChange: function (currentPage) {
            //跳转到第几页 --好像只针对用户暂时
            this.pageNo = currentPage;
            if (this.leftNum == 0) {
                this.ajaxData(currentPage);
            } else if (this.leftNum == 4) {
                this.proTableajax();
            }

        }, ajaxData(pageNo) {
            //查询用户
            var selecName = {
                pageNo: pageNo,
                userName: this.queryText,
            };
            //获取表格数据
            var result;
            this.$http.post('../user/queryList.do', selecName, {emulateJSON: true}).then(function (res) {
                result = res.data;
                this.totalCont = result.counts;
                this.usertableData = result.list;
            });
        }, crawlerFloseNumber() {
            //判断开启爬虫的button是否可点
            this.$http.get('../crawler/CrawlerTheadFlose.do', {emulateJSON: true}).then(function (res) {
                this.CrawlerFlose = res.data.Crawler;
                this.HardwareFlose = res.data.Hardware;
                this.activeImageFlose = true;

                // if(res.data.Crawler!=null){
                //     this.activeCrlawerFlose = true;
                //     this.crawlerButText = "已开启爬虫数据"
                // }
                // if(res.data.Image!=null){
                //     this.activeImageFlose = true;
                //     this.imageButText = "已开启卫星数据下载"
                // }
                // if(res.data.Hardware!=null){
                //     this.hardwareButText="已开始定时接入数据";
                //     this.activeHardwareFlose=true;
                // }

            });
        },
        openHardware() {
            //开启硬件接入
            // this.$http.get('../hardware/HardwareThead.do', {emulateJSON: true}).then(function (res) {
            //     console.log(res.data)
            // });
            this.$message({
                type: 'success',
                message: '开启一次硬件下载',
            });
            this.$http.get('../hardware/beginDownLoad.do', {emulateJSON: true}).then(function (res) {

            });


            // tihs.activeHardwareFlose=true;
        }, updataUser(row) {
            //修改用户弹窗
            this.UserFormUpdata = row;
            this.dialogFormVisible = true;
        }, updataCralwer(row) {
            //修改爬虫弹窗
            this.formCrawlerUpdate = row;
            this.crawlerDialogFormVisible = true;
        }, updataImage(row) {
            //修改影像弹窗
            this.formImageUpdate = row;
            this.imageDialogFormVisible = true;
        }, subUser() {
            //修改用户确定
            this.dialogFormVisible = false;
            var reusltData = JSON.parse(JSON.stringify(this.UserFormUpdata));
            delete reusltData.createTime;
            delete reusltData.updateTime;
            this.$http.post(('../user/addUser.do'), reusltData, {emulateJSON: true}).then(response => {
                // console.log(response.body);
                this.$message({
                    message: '成功！',
                    type: 'success'
                });
                this.ajaxData(this.pageNo);
            }, response => {
                this.$message.error('失败！');
            });
        }, subCrawler() {
            //修改用户确定
            this.crawlerDialogFormVisible = false;
            var reusltData = JSON.parse(JSON.stringify(this.formCrawlerUpdate));
            delete reusltData.crawlerTime;
            this.$http.post(('../crawler/updateCrawler.do'), reusltData, {emulateJSON: true}).then(response => {
                // console.log(response.body);
                this.$message({
                    message: '成功！',
                    type: 'success'
                });
                this.crawlerTableajax();
            }, response => {
                this.$message.error('失败！');
            }).catch();
        }, subImage() {
            //修改影像信息

            var reusltData = JSON.parse(JSON.stringify(this.formImageUpdate));
            delete reusltData.beginTime;
            this.$http.post(('../crawler/updateImageCrawler.do'), reusltData, {emulateJSON: true}).then(response => {
                // console.log(response.body);
                this.$message({
                    message: '成功！',
                    type: 'success'
                });
                this.imageDialogFormVisible = false;
                this.imageTableajax();
            }, response => {
                this.$message.error('失败！');
            }).catch();

        }, deleteUser(stats, row) {
            //0 是离职  1是在职
            //离职或者复职用户
            console.log("离职")
            var statsText = "";
            if (this.locals18n == "en" && stats == 0) {
                statsText = messages.en.homeSystem.Resignation_reminder;
            } else if ("en" == this.locals18n && stats == 1) {
                statsText = messages.en.homeSystem.Reinstatement_reminder;
            } else if ("zh" == this.locals18n && stats == 0) {
                statsText = messages.zh.homeSystem.Resignation_reminder;
            } else if ("zh" == this.locals18n && stats == 1) {
                statsText = messages.zh.homeSystem.Reinstatement_reminder;
            }else{
                statsText = "错误";
            }
            this.$confirm(statsText, '', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                row.deleteFlag = stats;
                var reusltData = JSON.parse(JSON.stringify(row));
                delete reusltData.createTime;
                delete reusltData.updateTime;
                this.$http.post(('../user/addUser.do'), reusltData, {emulateJSON: true}).then(response => {
                    // console.log(response.body);
                    this.$message({
                        message: '修改成功！',
                        type: 'success'
                    });
                    this.ajaxData(this.pageNo);
                }, response => {
                    this.$message.error('修改失败！');
                }).catch();
            });
        }, queryName() {  //查找name
            this.ajaxData(this.currentPage);
            this.queryText = "";
        }, addUser() {
            //添加用户
            this.UserFormUpdata = {
                id: "",
                userName: "",
                name: '',
                email: '',
                passWord: "",
                nickName: "",
                sex: "0",
                phone: ""
            },
                this.dialogFormVisible = true;
        }, activateTask(stats, row) {


            var statsText = "";
            if ("en" == this.locals18n && stats == 0) {
                statsText = messages.en.homeSystem.Startup_prompt;
            } else if ("en" == this.locals18n && stats == 1) {
                statsText = messages.en.homeSystem.stop_prompt;
            } else if ("zh" == this.locals18n && stats == 0) {
                statsText = messages.zh.homeSystem.Startup_prompt;
            } else if ("zh" == this.locals18n && stats == 1) {
                statsText = messages.zh.homeSystem.stop_prompt;
            }
            this.$confirm(statsText, '', {
                confirmButtonText: 'ok',
                cancelButtonText: 'cancel',
                type: 'warning'
            }).then(() => {
                var id = row.id;
                this.$http.get('../crawler/activateTask.do?id=' + id + "&&state=" + stats, {emulateJSON: true}).then(function (res) {
                    this.crawlerTableajax();
                    this.$message({
                        type: 'success',
                        message: '成功!'
                    });
                });
            }).catch();
        }, openCrawler() {
            //开启一次 爬虫
            this.$message({
                type: 'success',
                message: "开启一次爬虫下载",
            });
            this.$http.get('../crawler/beginCrawler.do', {emulateJSON: true}).then(function (res) {

                // if (this.CrawlerFlose == 0) {
                //     this.CrawlerFlose = 1;
                // } else {
                //     this.CrawlerFlose = 0;
                // }
            });


            // this.$message({
            //     type: 'success',
            //     message: '开启成功!'
            // });
            //   this.crawlerTableajax();
            // this.activeCrlawerFlose=true;

        }, openImage() {
            //开启爬虫

            // this.$message({
            //     message: '因服务器存储容量有限，该功能关闭！',
            //     type: 'error'
            // });

            // this.$http.get('../crawler/beginImageCrawler.do', {emulateJSON: true}).then(function (res) {
            // });
            this.$message({
                type: 'success',
                message: '开启成功!'
            });
            this.activeImageFlose = true;

        }, reallyTranslations(name) {
            if (this.leftNum == 1) {
                //爬虫管理
                if (name == "1") {
                    name = "启动";
                } else if (name == "0") {
                    name = "停止"
                }
            } else if (this.leftNum == 4) {
                if (name == "1") {
                    name = "硬件";
                } else if (name == "0") {
                    name = "模拟"
                }
            }
            if ("en" == this.locals18n) {
                switch (name) {
                    case "男":
                        name = messages.en.homeSystem.male;
                        break;
                    case "女":
                        name = messages.en.homeSystem.Female;
                        break;
                    case "水质数据":
                        name = messages.en.MonitoringTable.Water_quality_data;
                        break;
                    case "太阳辐射":
                        name = messages.en.MonitoringTable.Sun_radiation;
                        break;
                    case "潮汐数据":
                        name = messages.en.MonitoringTable.Tide_data;
                        break;
                    case "香港天文台":
                        name = messages.en.MonitoringTable.Hong_Kong_Observatory;
                        break;
                    case "深圳气象数据网":
                        name = messages.en.MonitoringTable.Shenzhen_Meteorological;
                        break;
                    case '启动':
                        name = messages.en.homeMonitoring.start_up;
                        break;
                    case '停止':
                        name = messages.en.homeMonitoring.time_out;
                        break;
                    case '模拟':
                        name = messages.en.homeMonitoring.Simulation_data;
                        break;
                    case '硬件':
                        name = messages.en.homeMonitoring.observation_dat;
                        break;
                }
            }
            return name;
        },
        proSweet(stats, row) {
            //修改属性
            this.proFromUpdata = row;
            this.redRange = this.ParsingRed(row.proRed);
            this.yellowRange = this.ParsingRed(row.proYellow);
            this.greenRange = this.ParsingRed(row.proGreen);
            this.proLayer = true;
        },
        ParsingRed(value) {
            //解析范围
            // [+∞,2000000]&[0,-∞]
            // console.log(value);
            if (value == null) {
                var addValue = [{
                    max: "+∞",
                    min: "-∞",
                }];
                return addValue;
            } else {
                var valueSpile = value.split("&");
                var maxJsonArray = [];
                for (let i = 0; i < valueSpile.length; i++) {
                    var maxQuJian = valueSpile[i].substring(1, valueSpile[i].length - 1).split(",");
                    var  max=maxQuJian[0];
                    var  min=maxQuJian[1];
                    if (typeof (max)=="undefined"||max.match(/^[ ]*$/)) {
                        max= "+∞"
                    }
                    if (typeof (min)=="undefined"||min.match(/^[ ]*$/)) {
                        min= "-∞"
                    }
                    var jsonObject = {
                        max: max,
                        min: min,
                    };
                    maxJsonArray.push(jsonObject);
                }
                return maxJsonArray;
            }

        },
        adddProRange(type) {
            var addValue = {
                max: "+∞",
                min: "-∞",
            };
            if (type == 0) {
                this.redRange.push(addValue);
            } else if (type == 1) {
                this.yellowRange.push(addValue);
            } else if (type == 2) {
                this.greenRange.push(addValue);
            }
        },
        subPro() {
            //提交修改属性信息
            var redRangeValue = this.zhuhePro(this.redRange);
            var yellowRangeValue = this.zhuhePro(this.yellowRange);
            var greenRangeValue = this.zhuhePro(this.greenRange);
            // console.log(redRangeValue);
            // if (redRangeValue == "" || redRangeValue == null) {
            //     redRangeValue = this.proFromUpdata.proRed;
            // }
            // if (yellowRangeValue == "" || yellowRangeValue == null) {
            //     yellowRangeValue = this.proFromUpdata.proYellow;
            // }
            // if (greenRangeValue == "" || greenRangeValue == null) {
            //     greenRangeValue = this.proFromUpdata.proGreen;
            // }
            console.log("最后传输的数据"+redRangeValue);
            console.log("最后传输的数据"+yellowRangeValue);
            console.log("最后传输的数据"+greenRangeValue);
            this.$http.get('../readMap/updatePro.do?id=' + this.proFromUpdata.id + "&redRange=" + redRangeValue + "&yellowRange=" + yellowRangeValue + "&greenRange=" + greenRangeValue + "&unit=" + this.proFromUpdata.proUnit, {emulateJSON: true}).then(function (res) {
                this.proTableajax();
            });

            // console.log(redRangeValue,yellowRangeValue,greenRangeValue);
        },
        zhuhePro(josnArray) {

            var result = "";
            for (let i = 0; i < josnArray.length; i++) {
                var proMaxMin = "[" + josnArray[i].max + "," + josnArray[i].min + "]";
                if (josnArray[i].max == "+∞" && josnArray[i].min == "-∞") {
                    continue;
                } else if (josnArray[i].max.match(/^[ ]*$/) || josnArray[i].min.match(/^[ ]*$/)) {
                    this.$message({
                        message: '请不要输入空格！',
                        type: 'error'
                    });
                    continue;
                } else if ((!isNaN(josnArray[i].max) || josnArray[i].max == "+∞") && (!isNaN(josnArray[i].min) || josnArray[i].min == "-∞")) {
                    result = result + "@" + proMaxMin;
                } else {
                    // console.log("违规" + josnArray[i].max + ";" + josnArray[i].min);
                    this.$message({
                        message: '请输入正常数字！',
                        type: 'error'
                    });
                    continue;
                }
            }
            // console.log("解析完毕"+result);
            // console.log(josnArray);
            return result.substring(1, result.length);
        }
    }
});